package com.cskaoyan.service;

import com.cskaoyan.bean.po.MarketRegion;
import com.cskaoyan.bean.po.MarketRegionExample;
import com.cskaoyan.bean.vo.admin_region.AdminRegionControllerVO;
import com.cskaoyan.bean.vo.admin_region.ChildrenListVO;
import com.cskaoyan.bean.vo.admin_region.CityListVO;
import com.cskaoyan.bean.vo.admin_region.ProvinceListVO;
import com.cskaoyan.mapper.MarketRegionMapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * @Author: Ryan
 * @CreateTime: 2022-09-06  18:27
 */
@Service
public class AdminRegionServiceImpl implements AdminRegionService{
    @Autowired
    MarketRegionMapper marketRegionMapper;

    @Override
    public AdminRegionControllerVO getAdminRegionList() {

        MarketRegionExample marketRegionExample = new MarketRegionExample();

        // marketRegionExample.setOrderByClause(sort+" "+order);

        List<MarketRegion> marketRegions = marketRegionMapper.selectByExample(marketRegionExample);
        List<ProvinceListVO> provinceList = new LinkedList<>();

        //通过pageInfo来获得分页数据！！
        PageInfo<MarketRegion> marketRegionPageInfo = new PageInfo<>(marketRegions);

        int page = marketRegionPageInfo.getPageNum();
        int limit = marketRegionPageInfo.getPageSize();//是默认的limit

        //获得total
        int total = marketRegions.size();
        //获得pages
        int pages = total/limit + 1;

        for (MarketRegion marketRegion : marketRegions) {
            //若为省 则逐级插入
            if (marketRegion.getPid() == 0) {

                //省份
                ProvinceListVO provinceListVO = new ProvinceListVO();
                provinceListVO.setId(marketRegion.getId());
                provinceListVO.setName(marketRegion.getName());
                provinceListVO.setType(marketRegion.getType());
                provinceListVO.setCode(marketRegion.getCode());

                //城市  通过省份的ID作为连接到城市的pid 获取这个省的所有城市
                List<MarketRegion> marketRegion2 = marketRegionMapper.selectCityByPid(provinceListVO.getId());

                List<CityListVO> cityList = new LinkedList<>();
                for (MarketRegion region : marketRegion2) {
                        //城市
                        CityListVO cityListVO = new CityListVO();
                        cityListVO.setId(region.getId());
                        cityListVO.setName(region.getName());
                        cityListVO.setType(region.getType());

                        //辖区 通过城市的ID作为连接到辖区的pid 获取这个城市的所有辖区  例如：洪山区 江夏区
                    List<ChildrenListVO> childrenList = marketRegionMapper.selectChildrenByPid(region.getId());
                    cityListVO.setChildren(childrenList);
                    cityList.add(cityListVO);
                }

                provinceListVO.setChildren(cityList);
                provinceList.add(provinceListVO);
            }
        }

        AdminRegionControllerVO adminRegionControllerVO = new AdminRegionControllerVO(total,pages,limit,page,provinceList);
        return adminRegionControllerVO;
    }
}
